package io.gitee.bowen_lee.netty.bowen.client;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;

/**
 * handle 适配器, 用来处理R/W
 * 
 * @author Bowen
 * @since 2022-01-10 16:33:55
 */
public abstract class MyChannelHandle extends ChannelInboundHandlerAdapter {

  @Override
  public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    ByteBuf buf = (ByteBuf) msg;
    // read 方法会动指针
    // CharSequence charSequence = buf.readCharSequence(buf.readableBytes(),
    // CharsetUtil.UTF_8);

    // get 不会动指针
    CharSequence charSequence = buf.getCharSequence(0, buf.readableBytes(), CharsetUtil.UTF_8);
    System.out.println("收到" + charSequence.toString());
    println(ctx, charSequence.toString());// 发送内容
  }

  /**
   * 发送啥啊
   */
  public abstract void println(ChannelHandlerContext ctx, String msgStr);

}
